#include "s_sys_init.h"

#include "s_sc_uart.h"
#include "h_cc2640r2f_define.h"

#include "h_cc2640r2f_iics.h"
#include "h_cc2640r2f_simpletime.h"

#include "h_cc2640r2f_uart.h"

#include <ti/devices/cc26x0r2/driverlib/ioc.h>

#include <ti/sysbios/knl/Task.h>
#include <ti/sysbios/knl/Clock.h>
#include <ti/sysbios/knl/Event.h>
#include <ti/sysbios/knl/Queue.h>
#include <ti/sysbios/knl/Semaphore.h>

#include <ti/sysbios/BIOS.h>

#include <icall_ble_api.h>

#include "bsec_iot.h"

#include "util.h"

static signed char s_sys_bme680_bsec_init_rslt = -10;

signed char S_Sys_Init(void)
{
    //unsigned char *datatest = NULL;

    S_SC_UART_Init();
    //H_CC2640R2F_UART_Init();
    //H_CC2640R2F_DisplayUart_Init();
    
    //H_CC2640R2F_UART_Debug("Delay timer create %d\r\n", H_CC2640R2F_SimpleTime_Delayms_TimerCreate());

    H_CC2640R2F_IICS_Init();


    //H_CC2640R2F_IICS_Test();
    
    //S_BME680_Init();
    

    H_CC2640R2F_UART_Debug("The bsec init rslt is %d\r\n", s_sys_bme680_bsec_init_rslt);


    //S_BME680_Init();

    H_CC2640R2F_UART_Debug("System init success\r\n");

    //S_SC_UART_WriteBuff("Get\r\n", 5);

    /*
    datatest = (unsigned char *)ICall_malloc(sizeof(unsigned char) * 644);
    if(NULL == datatest)
    {
        H_CC2640R2F_UART_Debug("Malloc wrong\r\n");
    }
    H_CC2640R2F_UART_Debug("Malloc finished\r\n");
    */

    return 0;
}

#define bme680TaskMd_Task_Stack_Size    3072//1536//2048//768//1536//1536//4096//1536
// Task configuration
//Task_Struct bme680TaskMd_Task;
Task_Handle bme680_TaskHandle = NULL;

//signed char bme680_task_handle_rslt = 98;

//
//#if defined __TI_COMPILER_VERSION__
//#pragma DATA_ALIGN(bme680TaskMd_Task_Stack, 8)
//#else
//#pragma data_alignment=8
//#endif
char bme680TaskMd_Task_Stack[bme680TaskMd_Task_Stack_Size];




//#define BME680_TimeEvent                    Event_Id_00
//
//
//#define BME680_AllEvent                     BME680_TimeEvent
//
//static Event_Handle bme680_eventhandle;
//static Event_Struct bme680_eventstruct;

#include "h_cc2640r2f_simpletime.h"

Clock_Handle bme680_TimerHandle = NULL;

int64_t bme680_TimerCount = 0;
//
static void s_bme680_timerhandle_callbackFun(UArg arg0)
{
    bme680_TimerCount++;
}

int64_t get_bme680_timercount_us(void)
{
    return (bme680_TimerCount);
}


static Clock_Struct bme680Clock;

static void bme680_timer_create(void)
{
    Util_constructClock(&bme680Clock, s_bme680_timerhandle_callbackFun,
                            100, 0, false, NULL);
    Util_startClock(&bme680Clock);

}

static void bme680_taskmode_taskFxn(UArg a0, UArg a1)
{

//    bme680_TimerHandle = H_CC2640R2F_SimpleTime_TimerCreate(1, 1, &s_bme680_timerhandle_callbackFun);
//    if(NULL == bme680_TimerHandle)
//    {
//        H_CC2640R2F_UART_Debug("BME680 timer handle create failed\r\n");
//    }
//    else
//    {
//        H_CC2640R2F_SimpleTime_TimerStart(bme680_TimerHandle);
//    }

//    bme680_timer_create();

    S_BME680_Init();

    for(;;)
    {

        S_BME680_Loop();
    }

/*
    for(;;)
    {
//        H_CC2640R2F_UART_Debug("The Clock_tickPeriod is %d\r\n", Clock_tickPeriod);
//        H_CC2640R2F_UART_Debug("The Clock_getTickPeriod is %d\r\n", Clock_getTickPeriod);
//        Task_sleep(ICALL_TIMEOUT_FOREVER);


        H_CC2640R2F_UART_Debug("The timer is %d\r\n", H_CC2640R2F_SimpleTime_GetSystickms());
        Task_sleep(100);
    }
*/

}


void BME680_TaskMode_createTask(void)
{
    Task_Params taskParams;

//    Error_Block eb;
//    Error_init(&eb);

    // Configure task
    Task_Params_init(&taskParams);
    taskParams.stack = bme680TaskMd_Task_Stack;
    taskParams.stackSize = bme680TaskMd_Task_Stack_Size;
    taskParams.priority = 2;
    //Task_construct(&bme680TaskMd_Task, \
                   (Task_FuncPtr)bme680_taskmode_taskFxn, &taskParams, NULL);
    bme680_TaskHandle = Task_create((Task_FuncPtr)bme680_taskmode_taskFxn, \
                                        &taskParams, NULL);
    if(NULL == bme680_TaskHandle)
    {
        H_CC2640R2F_UART_Debug("BME680 Task init failed\r\n");

        //bme680_task_handle_rslt = 5;
    }
    else
    {
        H_CC2640R2F_UART_Debug("BME680 Task init success\r\n");

        //bme680_task_handle_rslt = 9;
    }
}


void S_Sys_Init_CreateTask(void)
{
    //S_BME680_Init();

    BME680_TaskMode_createTask();
}


void S_Sys_BME680_bsec_init(void)
{
    s_sys_bme680_bsec_init_rslt = S_BME680_bsec_lib_init();
}


